// pages/classroom/reservation_details/reservation_details.js
import request from "../../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cateArray:[
      {id:'1', cate:'教学'},{id:'2', cate:'自习'},{id:'3',cate:'班会'},{id:'4',cate:'团日活动'},{id:'5',cate:'学生组织活动'},{id:'6',cate:'其他'}],
      cateIndex:0,
      cname:'教学',
      classroomId:'',
      date:'',
      week:'',
      period:'',
      userId:''
    },

    bindCatePickerChange: function (e) {
      var cname=this.data.cateArray[e.detail.value].cate;
      this.setData({
        cateIndex: e.detail.value, 
        cname:cname    
      }) 
    },

    save:function(e){
      var date = {'classroom':this.data.classroomId,'time':this.data.date,'week':this.data.week,'period':this.data.period,'purpose':this.data.cname,'user':this.data.userId};
      console.log(JSON.stringify(date))
      wx.request({
        url:"http://localhost:8080/classroom_ms1.0/reservation",
        method:"POST",
        data:JSON.stringify(date),
        header: {
          'content-type': 'application/json', // 默认值
          'token':wx.getStorageSync('token')
        },
        success (data){
          console.log(data.data)
          wx.showToast({
            title: data.data.msg,
            icon: 'success',
            duration: 2000
          })
          setTimeout(function() {
            wx:wx.navigateBack({
              delta: 1,
            })
         }, 2000);  
         
        },
        fail (err){
            reject(err)
        }
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var userInfo = JSON.parse(wx.getStorageSync('userInfo'));
      this.setData({
        classroomId:options.classroom_id,
        week:options.week,
        date:options.date,
        period:options.peroid,
        userId:userInfo.userid
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})